﻿
Namespace PL.MESSDK
    Public Class BOMRevisionDetail_PL
        Inherits PLBase
        Implements IPLProcess

        Public Property ItemMasterID_FK As Guid?
        Public Property BOMRevID_FK As Guid?
        Public Property PPrdId_FK As Guid?

        Public Sub New(ByVal queryName As String)
            MyBase.New(queryName)
        End Sub

        Public Sub New()
            MyBase.New()
        End Sub

        Sub OnRowUpdating(ByVal sender As Object, ByVal e As SqlRowUpdatingEventArgs) Handles MetaDataTable.OnRowUpdating
        End Sub

        Public Overrides Function Delete() As Boolean
            'Throw New NotImplementedException("Delete To Come...")
            Return False
        End Function

        Public Overrides Function Validate() As Boolean
            'Throw New NotImplementedException("Validate To Come...")
            Return True
        End Function

        Public Overrides Sub ValidateRow(row As DataRow)
            row.ClearErrors()

            If IsDBNull(row("Target")) Then
                row.SetColumnError("Target", "Mandatory")
            End If

            If row.HasErrors Then
                row.RowError = "Please check your columns, a validation error occur"
            End If
        End Sub

        'Public Overrides Function InitData(Optional withChild As Boolean = True) As Boolean
        '    Dim aDR As DataRelation
        '    Dim metaDataTableChild As CommonExt.MetaDataTable

        '    Dim relationName As String
        '    Dim enableConstraint As Boolean
        '    Dim aParentRelationField As String
        '    Dim aPreviousParentRelationField As String
        '    Dim aChildRelationField As String

        '    Try
        '        Me.DataSet = New DataSet()
        '        MetaDataTable = New MetaDataTable()

        '        Me.MetaDataTable.QueryName = Me.QueryName
        '        Me.MetaDataTable.FillBusinessApplicationData()

        '        Me.DataSet.Tables.Add(Me.MetaDataTable)
        '        If withChild Then
        '            aPreviousParentRelationField = String.Empty
        '            For Each row As DataRow In Me.MetaDataTable.MetaCommand.GetChildDisplayQuery.Rows

        '                metaDataTableChild = New CommonExt.MetaDataTable() With {.QueryName = row("QueryName").ToString}
        '                metaDataTableChild.FillBusinessApplicationData()
        '                Me.DataSet.Tables.Add(metaDataTableChild)
        '                '
        '                relationName = row("Relation").ToString
        '                aParentRelationField = row("ParentGUIDField").ToString
        '                aChildRelationField = String.Format("{0}_FK", aParentRelationField)
        '                enableConstraint = True
        '                '
        '                Select Case relationName
        '                    Case "IMBItemMaster/IMBItemMasterRev/IMBRevision"
        '                        enableConstraint = False
        '                        'ItemMasterSpecRevID
        '                        aParentRelationField = "BOMRevID_FK"
        '                        aChildRelationField = "BOMRevID"
        '                    Case ""

        '                    Case ""

        '                    Case Else
        '                        enableConstraint = True
        '                End Select

        '                aDR = New DataRelation(relationName,
        '                                        Me.DataSet.Tables(row("ParentQueryName").ToString).Columns(aParentRelationField),
        '                                        metaDataTableChild.Columns(aChildRelationField),
        '                                        enableConstraint)
        '                Me.DataSet.Relations.Add(aDR)
        '                '
        '                aPreviousParentRelationField = aParentRelationField
        '            Next
        '        End If
        '        Return True
        '    Catch ex As Exception
        '        Throw ex
        '    End Try
        '    Return False
        'End Function

        Public Shared Sub BOMRevisionDetail_Limite(aDataRow As DataRow)

            'TODO...
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("IMBRDetail_Revise")
                'Match les param[etre de la SP avec les colonne de la grilles
                For Each pr As System.Data.SqlClient.SqlParameter In sqlcmd.Parameters
                    If aDataRow.Table.Columns.Contains(pr.SourceColumn) Then
                        sqlcmd.Parameters(pr.ParameterName).Value = aDataRow(pr.SourceColumn)
                    End If
                Next
                sqlcmd.ExecuteNonQuery()
                'Fin TODO
            Catch sqlex As System.Data.SqlClient.SqlException

            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Dispose()
                End If
            End Try

        End Sub

        Public Shared Sub IMRevision_Copy(ByVal ItemMasterRevId As Guid, ByVal ItemMasterID_FK As Guid)

            'TODO...
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("IMRevision_CopyFrom")
                'Match les param[etre de la SP avec les colonne de la grilles

                sqlcmd.Parameters("@ItemMasterRevId").Value = ItemMasterRevId
                sqlcmd.Parameters("@ItemMasterID_FK").Value = ItemMasterID_FK

                sqlcmd.ExecuteNonQuery()
                'Fin TODO
            Catch sqlex As System.Data.SqlClient.SqlException

            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Dispose()
                End If
            End Try

        End Sub

        Public Shared Sub IMBOMRevision_Copy(ByVal ItemMasterSpecRevID As Guid, ByVal ItemMasterRevId As Guid)
            'TODO...
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("IMBRevision_CopyFrom")

                sqlcmd.Parameters("@BOMRevID").Value = ItemMasterSpecRevID
                sqlcmd.Parameters("@ItemMasterRevId").Value = ItemMasterRevId

                sqlcmd.ExecuteNonQuery()
                'Fin TODO
            Catch sqlex As System.Data.SqlClient.SqlException

            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Dispose()
                End If
            End Try

        End Sub

        Public Function BatchRecords(dataRow() As System.Data.DataRow) As Boolean Implements IPLProcess.BatchRecords
            Return True
        End Function

        Public Function CurrentRecord(dataRow As System.Data.DataRow) As Boolean Implements IPLProcess.CurrentRecord
            Return True
        End Function

        Public Function NewRecord(dataRow As System.Data.DataRow) As Boolean Implements IPLProcess.NewRecord
            'Si on arrive d'un TreeList...
            If ItemMasterID_FK IsNot Nothing Then
                dataRow("ItemMasterID_FK") = ItemMasterID_FK
            End If

            If BOMRevID_FK IsNot Nothing Then
                dataRow("BOMRevID_FK") = BOMRevID_FK
            End If

            If PPrdId_FK IsNot Nothing Then
                dataRow("PPrdId_FK") = PPrdId_FK
            End If

            If IsDBNull(dataRow("SOP")) Then
                dataRow("SOP") = "00000"
            End If

            If IsDBNull(dataRow("Method")) Then
                dataRow("Method") = String.Empty
            End If

            If IsDBNull(dataRow("Scaling")) Then
                dataRow("Scaling") = 0
            End If

            If IsDBNull(dataRow("Alternate")) Then
                dataRow("Alternate") = 0
            End If

            If IsDBNull(dataRow("BegRev")) Then
                dataRow("BegRev") = 0
            End If

            If IsDBNull(dataRow("EndRev")) Then
                dataRow("EndRev") = 9999
            End If




            dataRow("BOMId") = Guid.NewGuid
            dataRow("BOMRevDetailID") = Guid.NewGuid

            Return True
        End Function

    End Class
End Namespace